setPage();
    $('.container').click(function(e){
      let cart =JSON.parse(window.localStorage.getItem('cart'));
      if(e.target.getAttribute('name')=== 'all'){
         cart.forEach(item=>{
        item.buy=$(e.target).prop('checked');
         })
      }else if(e.target.getAttribute('name')=== 'other'){
        for(var i=0;i<=cart.length-1;i++){
          if(cart[i].goods_id === e.target.getAttribute('goods_id')){
          cart[i].buy =$(e.target).prop('checked');
          }
        }
      }else if(e.target.getAttribute('name')=== 'del'){
      for(var i=0;i<=cart.length-1;i++){
        if(cart[i].goods_id === e.target.getAttribute('goods_id')){
        cart.splice(i,1);
        }
      }
      }else if(e.target.getAttribute('name')==='-'){
       for(var i=0;i<=cart.length-1;i++){
         if(cart[i].goods_id === e.target.getAttribute('goods_id')){
           cart[i].num=cart[i].num-0-1;
         }
       }
      }else if(e.target.getAttribute('name')==='+'){
         for(var i=0;i<=cart.length-1;i++){
           if(cart[i].goods_id === e.target.getAttribute('goods_id')){
             cart[i].num=cart[i].num-0+1;
           }
         }
      }
      window.localStorage.setItem('cart',JSON.stringify(cart));
      setPage();
    });

     function setPage(){
       let cart =window.localStorage.getItem('cart');
       if(cart === null){
         window.alert('您的购物车是空车，请您添加购买商品');
       }else{
         cart=JSON.parse(cart);
         if(cart.length === 0){
          window.alert('您的购物车是空车，请您添加购买商品');
         }else{
           let bool=true;
           let type=0;
           let row=0;
           let money=0;
           //第一部分
           let str=
           `
          <div class="panel panel-info ">
          <div class="panel-body bg-info">
          <div class="checkbox">
          <label>
          <input name="all" type="checkbox" value="">
          全选
          </label>
          </div>
          </div>
          <div class="panel-footer">
          <ul class="cart-list">
           `;
          //第二部分
            cart.forEach(item=>{
              if(item.buy === false){
                bool=false;
              }else{
                type += 1;
                row += item.num-0;
                money += (item.num-0)*(item.goods_price-0);
              }
              str +=
              `
              <li class="cart-item">
          <div class="left">
            <input name="other" goods_id="${item.goods_id}" type="checkbox" ${item.buy ?'checked' :''}>
          </div>
          <div class="right">
            <div class="media">
              <div class="media-left">
                <a href="#">
                  <img class="media-object" src="${item.goods_small_logo}" alt="...">
                </a>
              </div>
              <div class="media-body">
                <h4 class="media-heading">${item.goods_name}</h4>
                <p>
                  <i class="glyphicon glyphicon-yen"></i>
                  <span>${item.goods_price}</span>
                </p>
                <div class="btn-group pull-right" role="group" aria-label="...">
                  <button name="-" goods_id="${item.goods_id}" type="button" class="btn btn-default" ${item.num-0 === 1 ? 'disabled' :''}>-</button>
                  <button type="button" class="btn btn-default" disabled>${item.num}</button>
                  <button name="+" goods_id="${item.goods_id}" type="button" class="btn btn-default" ${item.num-0 === item.goods_number-0 ? 'disabled' : ''}>+</button>
                </div>
                <button name="del" goods_id="${item.goods_id}" class="del btn btn-danger">我不要了</button>

              </div>
            </div>
          </div>
        </li> 
              `;
          })
              //第三部分
              str +=
              `
            </ul>
            </div>
            </div>
            
            <div class="bar-wrapper">
            <div class="bar-right">
                <div class="piece">已选商品种类<strong class="piece_num">${type}</strong>种</div>
                <div class="piece">已选商品<strong class="piece_num">${row}</strong>件</div>
                <div class="totalMoney">共计: <strong class="total_text">${money.toFixed(2)}</strong></div>
                <div class="calBtn"><a href="javascript:;">结算</a></div>
            </div>
        </div>
              `;
            $('.container').html(str);
            $('[name="all"]').prop('checked',bool);
         }
       }
     }